package com.cdd.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdd.index.mapper.ZoneMapper;
import com.cdd.index.service.IZoneService;
import com.common.entity.Zone;
import com.common.entity.ZoneGoods;
import com.common.untils.CommonResult;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 专区 服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-02
 */
@Service
public class ZoneServiceImpl extends ServiceImpl<ZoneMapper, Zone> implements IZoneService {

    @Autowired
    private ZoneMapper zoneMapper;
    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, Zone zone) {
        Page<Zone> page =new Page<>(currentPage,pageSize);
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(zone.getZoneName())){
            wrapper.like("zone_name",zone.getZoneName());
        }
        if (zone.getIsDisable()!=null){
            wrapper.eq("is_disable",zone.getIsDisable());
        }
        Page<Zone> zonePage = zoneMapper.selectPage(page, wrapper);
        return new CommonResult(2000,"查询成功",zonePage);
    }

    @Override
    public CommonResult zoneAdd(Zone zone) {
        zone.setIsDeleted(0);
        zone.setIsDisable(0);
        if (StringUtils.hasText(zone.getId())){
            zoneMapper.updateById(zone);
            return new CommonResult(2000,"修改成功",null);
        }else {
            zoneMapper.insert(zone);
            return new CommonResult(2000,"添加成功",null);

        }
    }

    @Override
    public CommonResult deleteZone(String id) {
        zoneMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",null);
    }

    @Override
    public CommonResult updateStatus(String id,Integer isDisable) {
        Zone zone = new Zone();
        zone.setId(id);
        zone.setIsDisable(isDisable);
        int i = zoneMapper.updateById(zone);
        return new CommonResult(2000,"修改状态成功",null);
    }

    @Override
    public CommonResult findAll() {
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
            wrapper.eq("is_disable",0);
            wrapper.eq("is_deleted",0);
        List<Zone> zoneList = zoneMapper.selectList(wrapper);
        return new CommonResult(2000,"查询成功",zoneList);
    }

    @Override
    public CommonResult getAllzone() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_disable",0);
        List<Zone> zones = zoneMapper.selectList(queryWrapper);
        if (zones==null){
            return new CommonResult(5000,"查询专区信息失败",null);
        }
        for (Zone z :zones) {
            List<Map> list =zoneMapper.selectZoneGoods(z.getId());
            if (list==null){
                return new CommonResult(5000,"查询商品信息错误",null);
            }
            System.out.println("======"+list);
            z.setZoneGoodsList(list);
        }
        return new CommonResult(2000,"查询成功",zones);
    }

}
